module(
    name = "rules_coreutils",
    version = "1.0.0-beta.3",
    bazel_compatibility = [
        ">=7.0.0",
    ],
    compatibility_level = 1,
)

bazel_dep(name = "toolchain_utils", version = "1.0.0-beta.9")
bazel_dep(name = "download_utils", version = "1.0.0-beta.2")
bazel_dep(name = "ape", version = "1.0.0-beta.6")

archive = use_repo_rule("@download_utils//download/archive:defs.bzl", "download_archive")

[
    archive(
        name = "coreutils-{}".format(triplet),
        srcs = ["entrypoint"],
        integrity = integrity,
        links = {
            "coreutils.exe" if "windows" in basename else "coreutils": "entrypoint",
        },
        strip_prefix = "coreutils-0.0.26-{}".format(basename.partition(".")[0]),
        urls = ["https://github.com/uutils/coreutils/releases/download/0.0.26/coreutils-0.0.26-{}".format(basename)],
    )
    for triplet, basename, integrity in (
        ("arm64-linux-gnu", "aarch64-unknown-linux-gnu.tar.gz", "sha256-hYfQ6ZLx9pwP4GMBYAH42kvJnETjIAV7X/zdpCk2QUI="),
        ("amd64-linux-gnu", "x86_64-unknown-linux-gnu.tar.gz", "sha256-ZKgqe7xNZcCjrvVsEnyW+GJ5zf0k+hVsTq9pXLAKvz4="),
        ("arm64-linux-musl", "aarch64-unknown-linux-musl.tar.gz", "sha256-9zGLFOMDjUDbraDY/hrE5zFJ0O+QYrvx2wHk3Gw3q/A="),
        ("amd64-linux-musl", "x86_64-unknown-linux-musl.tar.gz", "sha256-QpGVp3wmHqpt5Brd/bdj7pyMQNftcihOtaRI8z2uhp0="),
        ("amd64-windows-msvc", "x86_64-pc-windows-msvc.zip", "sha256-6qPkqxQZM4XBBBJ80t1uvzfZiz0gBeT/zoCdfqOO3uk="),
        ("arm64-macos-darwin", "aarch64-apple-darwin.tar.gz", "sha256-/A6CNYWmvOwPW443pH+wO+VtzDFgussN1hRGuANnFOU="),
    )
]

archive(
    name = "coreutils-amd64-macos-darwin",
    srcs = ["entrypoint"],
    integrity = "sha256-SswetVAuK/hMK1r9uBvNnKj5JpSgD0bzkbsHTxOabCo=",
    links = {"coreutils": "entrypoint"},
    strip_prefix = "coreutils-0.0.23-x86_64-apple-darwin",
    urls = ["https://github.com/uutils/coreutils/releases/download/0.0.23/coreutils-0.0.23-x86_64-apple-darwin.tar.gz"],
)

select = use_repo_rule("@toolchain_utils//toolchain/local/select:defs.bzl", "toolchain_local_select")

select(
    name = "coreutils",
    map = {
        "amd64-linux": "@coreutils-amd64-linux-musl",
        "arm64-linux": "@coreutils-arm64-linux-musl",
        "amd64-windows": "@coreutils-amd64-windows-msvc",
        "arm64-macos-darwin": "@coreutils-arm64-macos-darwin",
        "amd64-macos-darwin": "@coreutils-amd64-macos-darwin",
    },
)

export = use_extension("@toolchain_utils//toolchain/export:defs.bzl", "toolchain_export")
export.symlink(
    name = "coreutils",
    target = "@coreutils",
)

deb = use_repo_rule("@download_utils//download/deb:defs.bzl", "download_deb")

deb(
    name = "busybox-arm64-linux",
    srcs = ["busybox"],
    integrity = "sha256-C0+0zi0/0Woc11BTX5d1ugxC2GOeE9ZjUka6g6DUvc8=",
    strip_prefix = "bin",
    urls = ["http://ftp.uk.debian.org/debian/pool/main/b/busybox/busybox-static_1.35.0-4+b3_arm64.deb"],
)

deb(
    name = "busybox-amd64-linux",
    srcs = ["busybox"],
    integrity = "sha256-rMRMIHKVuGEU2kiV71Ouvxhr8839wmmloaCer6xqYNs=",
    strip_prefix = "bin",
    urls = ["http://ftp.uk.debian.org/debian/pool/main/b/busybox/busybox-static_1.35.0-4+b3_amd64.deb"],
)

resolved = use_repo_rule("@toolchain_utils//toolchain/resolved:defs.bzl", "toolchain_resolved")

which = use_repo_rule("@toolchain_utils//toolchain/local/which:defs.bzl", "toolchain_local_which")

[
    (
        resolved(
            name = "resolved-{}".format(tool),
            basename = tool,
            toolchain_type = "//coreutils/toolchain/{}:type".format(tool),
        ),
        which(
            name = "which-{}".format(tool),
            basename = tool,
        ),
    )
    for tool in (
        "busybox",
        "coreutils",
        "arch",
        "b2sum",
        "base32",
        "base64",
        "basename",
        "basenc",
        "cat",
        "chcon",
        "chgrp",
        "chmod",
        "chown",
        "chroot",
        "cksum",
        "comm",
        "cp",
        "csplit",
        "cut",
        "date",
        "dd",
        "df",
        "dir",
        "dircolors",
        "dirname",
        "du",
        "echo",
        "env",
        "expand",
        "expr",
        "factor",
        "false",
        "fmt",
        "fold",
        "head",
        "hostname",
        "hostid",
        "id",
        "install",
        "join",
        "link",
        "ln",
        "logname",
        "ls",
        "md5sum",
        "mkdir",
        "mkfifo",
        "mknod",
        "mktemp",
        "more",
        "mv",
        "nice",
        "nl",
        "nohup",
        "nproc",
        "numfmt",
        "od",
        "paste",
        "pathchk",
        "pinky",
        "pr",
        "printenv",
        "printf",
        "ptx",
        "pwd",
        "readlink",
        "realpath",
        "rm",
        "rmdir",
        "runcon",
        "seq",
        "sha1sum",
        "sha224sum",
        "sha256sum",
        "sha3sum",
        "sha384sum",
        "sha512sum",
        "shred",
        "shuf",
        "sleep",
        "sort",
        "split",
        "stat",
        "stdbuf",
        "stty",
        "sum",
        "sync",
        "tac",
        "tail",
        "tee",
        "test",
        "timeout",
        "touch",
        "tr",
        "true",
        "truncate",
        "uname",
        "unexpand",
        "uniq",
        "unlink",
        "users",
        "vdir",
        "wc",
        "who",
        "whoami",
        "yes",
    )
]

register_toolchains("//coreutils/toolchain/...")
